import { defineStore } from "pinia";

/**
 * 路由配置说明：
 *
 * 路由对象属性说明：
 * - path: 路由路径，用于页面访问的URL地址
 * - name: 路由名称，用于编程式导航和路由识别
 * - children: 子路由数组，用于构建嵌套路由结构
 *
 * meta 对象属性说明：
 * - title: 页面标题，显示在浏览器标签页和面包屑导航中
 * - zh_hk_title: 繁体中文标题（可选）
 * - en_title: 英文标题（可选）
 * - icon: 图标名称，用于菜单和导航中显示图标
 * - affix: 是否固定标签页，true表示该页面标签不可关闭
 * - requiresAuth: 是否需要身份验证，true表示需要登录才能访问
 * - isHidden: 是否在菜单中隐藏，true表示不在侧边栏菜单中显示
 */

const menuRoutes = [
  //   {
  //     path: "/admin",
  //     name: "dashboard",
  //     children: [],
  //     activeMenu: "/admin",
  //     meta: {
  //       title: "仪表盘",
  //       zh_hk_title: "儀表板",
  //       en_title: "Dashboard",
  //       icon: "monitor",
  //       affix: true,
  //       requiresAuth: true,
  //     },
  //   },
  {
    path: "/admin/courseSystem",
    name: "courseSystem",
    meta: {
      title: "课程管理",
      zh_hk_title: "課程管理",
      en_title: "Course Management",
      icon: "monitor",
      requiresAuth: true,
    },
    children: [
      {
        path: "/admin/courseSystem/courseOfMy",
        name: "user",
        activeMenu: "/admin/courseSystem/courseOfMy",
        meta: {
          title: "我的课程",
          zh_hk_title: "我的課程",
          en_title: "My Courses",
          icon: "user",
          requiresAuth: true,
        },
        children: [],
      },
      // {
      //   path: "/admin/courseSystem/classSchedule",
      //   name: "user",
      //   activeMenu: "/admin/courseSystem/classSchedule",
      //   meta: {
      //     title: "课程表",
      //     zh_hk_title: "課程表",
      //     en_title: "Timetable",
      //     icon: "user",
      //     requiresAuth: true,
      //     // isHidden: true,
      //   },
      //   children: [],
      // },
      // {
      //   path: "/admin/courseSystem/classRecord",
      //   name: "user",
      //   activeMenu: "/admin/courseSystem/classRecord",
      //   meta: {
      //     title: "上课记录",
      //     zh_hk_title: "上課記錄",
      //     en_title: "Class Record",
      //     icon: "user",
      //     requiresAuth: true,
      //     // isHidden: true,
      //   },
      //   children: [],
      // },
    ],
  },
  // 报名
  {
    path: "/admin/registration",
    name: "registration",
    meta: {
      title: "我报名的",
      zh_hk_title: "我報名的",
      en_title: "Registration",
      icon: "monitor",
      requiresAuth: true,
    },
    children: [
      {
        path: "/admin/registration/registrationList",
        name: "user",
        activeMenu: "/admin/registration/registrationList",
        meta: {
          title: "报名列表",
          zh_hk_title: "報名清單",
          en_title: "Registration List",
          icon: "user",
          requiresAuth: true,
        },
        children: [],
      },
      // {
      //   path: "/admin/registration/test",
      //   name: "user",
      //   activeMenu: "/admin/registration/test",
      //   meta: {
      //     title: "表格测试数据",
      //     zh_hk_title: "表格测试数据",
      //     en_title: "表格测试数据",
      //     icon: "user",
      //     requiresAuth: true,
      //   },
      //   children: [],
      // },
    ],
  },
  {
    path: "/admin/order",
    name: "order",
    meta: {
      title: "我的订单",
      zh_hk_title: "我的訂單",
      en_title: "My Orders",
      icon: "monitor",
      requiresAuth: true,
    },
    children: [
      {
        path: "/admin/order/courseOrder",
        name: "user",
        activeMenu: "/admin/order/courseOrder",
        meta: {
          title: "课程订单",
          zh_hk_title: "課程訂單",
          en_title: "Course Order",
          icon: "user",
          requiresAuth: true,
        },
      },
    ],
  },
  // Personal Profile  个人资料   個人資料
  {
    path: "/admin/personalProfile",
    name: "personalProfile",
    meta: {
      title: "个人资料",
      zh_hk_title: "個人資料",
      en_title: "Personal Profile",
      icon: "monitor",
      requiresAuth: true,
    },
    children: [
      {
        path: "/admin/personalProfile/accountManagement",
        name: "user",
        activeMenu: "/admin/personalProfile/accountManagement",
        meta: {
          title: "账号管理",
          zh_hk_title: "帳號管理",
          en_title: "Account Management",
          icon: "user",
          requiresAuth: true,
        },
        children: [],
      },
      {
        path: "/admin/personalProfile/studentInformation",
        name: "user",
        activeMenu: "/admin/personalProfile/studentInformation",
        meta: {
          title: "学员信息",
          zh_hk_title: "營員資訊",
          en_title: "Student Information",
          icon: "user",
          requiresAuth: true,
          requiresBizUserId: true,
        },
        children: [],
      },
      //   {
      //     path: "/admin/personalProfile/shippingAddress",
      //     name: "user",
      //     activeMenu: "/admin/personalProfile/shippingAddress",
      //     meta: {
      //       title: "收获地址",
      //       zh_hk_title: "收穫地址",
      //       en_title: "Shipping address",
      //       icon: "user",
      //       requiresAuth: true,
      //     },
      //     children: [],
      //   },
      {
        path: "/admin/personalProfile/customerService",
        name: "user",
        activeMenu: "/admin/personalProfile/customerService",
        meta: {
          title: "联系客服",
          zh_hk_title: "聯系客服",
          en_title: "customer service",
          icon: "user",
          requiresAuth: true,
        },
        children: [],
      },

      {
        path: "/admin/personalProfile/memberProfile",
        name: "user",
        activeMenu: "/admin/personalProfile/memberProfile",
        meta: {
          title: "会员资料",
          zh_hk_title: "會員資料",
          en_title: "Member profile",
          icon: "user",
          requiresAuth: true,
          requiresBizUserId: true,
        },
        children: [],
      },
      {
        path: "/admin/personalProfile/competitionRecord",
        name: "user",
        activeMenu: "/admin/personalProfile/competitionRecord",
        meta: {
          title: "参赛记录",
          zh_hk_title: "參賽記錄",
          en_title: "Competition record",
          icon: "user",
          requiresAuth: true,
          requiresBizUserId: true,
        },
        children: [],
      },
    ],
  },
  // {
  //   path: "/admin/system",
  //   name: "system",
  //   meta: {
  //     title: "系统管理",
  //     zh_hk_title: "系統管理",
  //     en_title: "System Management",
  //     icon: "setting",
  //     requiresAuth: true,
  //   },
  //   children: [
  //     {
  //       path: "/admin/system/user",
  //       activeMenu: "/admin/system/user",
  //       name: "user",
  //       meta: {
  //         title: "用户管理",
  //         zh_hk_title: "用戶管理",
  //         en_title: "User Management",
  //         icon: "user",
  //         requiresAuth: true,
  //       },
  //       children: [],
  //     },
  //     {
  //       path: "/admin/system/dept",
  //       activeMenu: "/admin/system/dept",
  //       name: "user",
  //       meta: {
  //         title: "部门管理",
  //         zh_hk_title: "部門管理",
  //         en_title: "Department Management",
  //         icon: "cluster",
  //         requiresAuth: true,
  //       },
  //       children: [],
  //     },
  //     {
  //       path: "/admin/system/role",
  //       activeMenu: "/admin/system/role",
  //       name: "team",
  //       meta: {
  //         title: "角色管理",
  //         zh_hk_title: "角色管理",
  //         en_title: "Role Management",
  //         icon: "team",
  //         requiresAuth: true,
  //       },
  //       children: [],
  //     },
  //     {
  //       path: "/admin/system/post",
  //       activeMenu: "/admin/system/post",
  //       name: "post",
  //       meta: {
  //         title: "岗位管理",
  //         zh_hk_title: "職位管理",
  //         en_title: "post Management",
  //         icon: "addteam",
  //         requiresAuth: true,
  //       },
  //       children: [],
  //     },
  //     {
  //       path: "/admin/test/test",
  //       activeMenu: "/admin/courseSystem/courseOfMy",
  //       name: "test",
  //       meta: {
  //         title: "测试表单",
  //         zh_hk_title: "测试表单",
  //         en_title: "post Management",
  //         icon: "addteam",
  //         requiresAuth: true,
  //       },
  //       children: [],
  //     },
  //   ],
  // },
];

const getBreadcrumbs = (routes: any[]) => {
  const breadcrumbs: any[] = [];
  const traverse = (routes: any[], parent: any = null) => {
    routes.forEach((route) => {
      const breadcrumb = {
        ...route.meta,
        path: route.path,
        parent: parent,
      };
      breadcrumbs.push(breadcrumb);
      if (route.children && route.children.length > 0) {
        traverse(route.children, breadcrumb);
      }
    });
  };
  traverse(routes);
  return breadcrumbs;
};

export const routerStore = defineStore("routerStore", {
  state: () => ({
    routes: menuRoutes,
    breadcrumbs: getBreadcrumbs(menuRoutes),
  }),
  getters: {
    indexRoute: (state) => {
      // state.routes
      return "/admin/courseSystem/courseOfMy";
    },
  },
  actions: {
    getBreadcrumbs(path: string) {
      let data: any = this.breadcrumbs.find((item: any) => item.path === path);
      const result = [];
      while (true) {
        result.unshift(data);
        if (data?.parent) {
          data = data.parent;
        } else {
          break;
        }
      }

      return result;
    },
    setRoutes(routers: []) {
      this.routes = routers;
    },
  },
});
